//
//  StatusCell.swift
//  MyWeibo
//
//  Created by tanchao on 15/8/6.
//  Copyright © 2015年 chaoren. All rights reserved.
//

import UIKit

let StatusCellControlMargin :CGFloat = 8.0
enum StatusCellIdentifier : String {
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    static func cellID(status:Status)->String{
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}
class StatusCell: UITableViewCell {
    /// 配图视图宽度约束
    var pictureViewConsWidth : NSLayoutConstraint?
    /// 配图诗图高度约束
    var pictureViewConsHeight : NSLayoutConstraint?
    /// 配图诗图顶部
    var pictureViewConsTop : NSLayoutConstraint?
    
    func rowHeight(status:Status)->CGFloat{
        self.status = status
        layoutIfNeeded()
        return  CGRectGetMaxY(bottomView.frame)
    
    }
    var status: Status?{
        didSet{
            topView.status = status!
            contentLabel.text = status?.text
            bottomView.status = status
            pictureView.status = status
            pictureViewConsHeight?.constant = pictureView.bounds.size.height
            pictureViewConsWidth?.constant = pictureView.bounds.size.width
            pictureViewConsTop?.constant = (pictureView.bounds.size.height == 0) ? 0 :StatusCellControlMargin
        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

      func prepareUI() {
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 2. 设置布局
        // 1> 顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        // 2> 内容标签
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: StatusCellControlMargin, y: StatusCellControlMargin))
        // 宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * StatusCellControlMargin))
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width , height: 5 * StatusCellControlMargin ), offset: CGPoint(x: -StatusCellControlMargin, y: StatusCellControlMargin))
        
    }
    //MARK:- 懒加载视图
    private lazy var topView:StatusTopView = StatusTopView()
    lazy var contentLabel:UILabel = UILabel(text_Color: UIColor.darkGrayColor(), font_Size: 15, number_OfLines: 0, preferredMaxWidth: UIScreen.mainScreen().bounds.width - 2 * StatusCellControlMargin)
    
    lazy var pictureView:StatusPictureView = StatusPictureView()
    lazy var bottomView:StatusBottomView = StatusBottomView()
    

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
